<template>
	<view class="pay-view">
		<icon-title color="#ffffff" comTitle="支付" desc></icon-title>
		<view class="order-pay-view">
			<view class="order-pay-box">
				<view class="order-pay-view1">
					<view class="order-pay-imagea">
						<!-- <image class="order-pay-image" src="../../static/index/108.png" mode=""></image> -->
						<text style="margin-left: 20rpx;">微信</text>
					</view>
					<view>
						<radio-group @change="radioChange">
							<radio value="wechart" color="#C52B21" :checked="radioValue == 'wechart'" />
						</radio-group>
					</view>
				</view>
			</view>
			<view class="order-pay-box">
				<view class="order-pay-view1">
					<view class="order-pay-imagea">
						<!-- <image class="order-pay-image" src="../../static/index/66.png" mode=""></image> -->
						<text style="margin-left: 20rpx;">支付宝</text>
					</view>
					<view>
						<radio-group @change="radioChange">
							<radio value="alipay" color="#C52B21" :checked="radioValue == 'alipay'" />
						</radio-group>
					</view>
				</view>
			</view>
			<!-- <view class="order-pay-box">
				<view class="order-pay-view1">
					<view class="order-pay-imagea">
						<image class="order-pay-image" src='../../static/index/107.png' mode=""></image>
						<text style="margin-left: 20rpx;">银行卡</text>
					</view>
					<view>
						<radio-group @change="radioChange">
							<radio value="code" color="#C52B21" :checked="radioValue == 'code'" />
						</radio-group>
					</view>
				</view>
			</view> -->
			<view class="order-pay-box" v-if="!form.amount">
				<view class="order-pay-view1">
					<view class="order-pay-imagea">
						<!-- <image class="order-pay-image" src='../../static/index/107.png' mode=""></image> -->
						<text style="margin-left: 20rpx;">余额</text>
					</view>
					<view class="order-pay-money">
						<view style="margin-right: 30rpx;">{{userInfo.user_money}}</view>
						<radio-group @change="radioChange">
							<radio value="money" color="#C52B21" :checked="radioValue == 'money'" />
						</radio-group>
					</view>
				</view>
			</view>
		</view>
		<!-- <template v-if="form.type == 3">
			<icon-title color="#ffffff" comTitle="支付金额" desc></icon-title>
			<view class="order-payPrcie-tabs">
				<view class="order-payPrice-tabItem" v-for="(item,index) in priceList" :key="index"
				@click="getCheckTabIndex(index,item)" :class="tabIndex==index?'accent-Tabcolor':'bg-Tabcolor'">{{item.title}}</view>
			</view>
		</template> -->
		<view class="pay-btn-view">
			<bottom-bottom name="确认" @click="createOrder"></bottom-bottom>
		</view>
	</view>
</template>

<script>
	import iconTitle from "@/components/title/icon-title.vue"
	import bottomBottom from "@/components/button/bottomButton.vue"
	export default {
		components: {
			iconTitle,
			bottomBottom
		},
		data() {
			return {
				radioValue: 'wechart',
				payId: 2,
				form: {},
				// priceList:[
				// 	{title:'100'},
				// 	{title:'200'},
				// 	{title:'300'},
				// 	{title:'500'}
				// ],
				tabIndex: 0,
				// 防抖
				isSubmit: true,
				timer: '',
				userInfo:{},
				form2:{}
			}
		},
		onLoad(option) {
			if (option.form) {
				this.form = JSON.parse(option.form)
				if(this.form.type == 6){
					uni.setNavigationBarTitle({
						title: '新春提现币支付'
					})
				}
				// if(this.form.type == 3){
				// 	this.form.price = '100'
				// }
			}
		},
		onShow() {
			this.getUserInfo()
		},
		methods: {
			// getCheckTabIndex(index,item){
			// 	this.tabIndex = index
			// 	this.form.price = item.title
			// },
			// navigatorPage(){
			// 	let path = ""
			// 	if(this.form.order_type == 1){
			// 		 path = "/pages/pay/ali-pay?form=" + JSON.stringify(this.form)
			// 	}else if(this.form.order_type == 2){
			// 		 path = "/pages/pay/wx-pay?form=" + JSON.stringify(this.form)
			// 	}else{
			// 		 path = "/pages/pay/bank-pay?form=" + JSON.stringify(this.form)
			// 	}
			// 	this.$tools.navigateTo(path)
			// },
			getUserInfo() {
				this.$http.post(this.$api.getUserInfo, {}, {
					token: true
				}).then(res => {
					this.userInfo = res.data
				})
			},
			radioChange(e) {
				this.radioValue = e.detail.value
				if (this.radioValue == 'wechart') {
					this.payId = 2
				} else if (this.radioValue == 'alipay') {
					this.payId = 1
				} else if (this.radioValue == 'code') {
					this.payId = 3
				} else if (this.radioValue == 'money') {
					this.payId = 6
				} else {
					this.payId = 2
				}
				this.form.order_type = this.payId
			},
			// 创建订单
			createOrder() {
				
				// let path = ""
				// if (this.radioValue == 'wechart') {
				// 	path = "/pages/pay/wx-pay?form=" + JSON.stringify(this.form)
				// } else if (this.radioValue == 'alipay') {
				// 	path = "/pages/pay/ali-pay?form=" + JSON.stringify(this.form)
				// } else if (this.radioValue == 'code') {
				// 	path = "/pages/pay/bank-pay?form=" + JSON.stringify(this.form)
				// } else if (this.radioValue == 'money') {
				// 	this.credentials()
				// } else {
				// 	path = "/pages/pay/wx-pay?form=" + JSON.stringify(this.form)
				// }
				// this.$tools.navigateTo(path)
				
				if(!this.isSubmit) return false
				this.isSubmit = false
				clearTimeout(this.timer)
				this.$http.post(this.$api.createOrder, this.form, {
					token: true
				}).then(res => {
					if (res.code == 1) {
						let urls =  'pay/pay?order_sn=' + res.data.order_sn
						this.$http.post(urls,{},{token:true}).then(ress=>{
							if(this.radioValue == 'money'){
								this.$tools.toast('订单已支付')
								this.getUserInfo()
								this.timer = setTimeout(function(){
									this.isSubmit = true
								},500)
								this.$tools.switchTab('/pages/initialShare/initialShare',500)
								return false
							}
							if(ress.code == 200){
								let path = '/pages/pay/pay-view?order=' + ress.data.payUrl
								this.$tools.reLaunch(path,200)
								this.timer = setTimeout(function(){
									this.isSubmit = true
								},500)
							}
						})
					}else{
						this.$tools.switchTab('/pages/index/index',500)
					}
				})
			},
			credentials(){
				// 创建订单
				this.$http.post(this.$api.createOrder,this.form,{token:true}).then(res=>{
					if(res.code == 1){
						this.form2.order_sn = res.data.order_sn
						this.credentialsData()
					}
				})
			},
			// 凭证方法
			credentialsData(){
				// clearTimeout(this.timer)
				this.$http.post(this.$api.credentials,this.form2,{token:true}).then(res=>{
					if(res.code == 1){
						this.$tools.toast('提交成功')
						this.getUserInfo()
						this.$tools.switchTab('/pages/index/index',500)
						// this.timer = setTimeout(function(){
						// 	this.isSubmit = true
						// },500)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.order-pay-money {
		display: flex;
		align-items: center;
		// justify-content: center;
	}

	// 金额切换
	.order-payPrcie-tabs {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 30rpx;
		background-color: #FFE6E6;
		border-radius: 30rpx;

		.order-payPrice-tabItem:first-child {
			margin-left: 0;
		}

		.order-payPrice-tabItem {
			padding: 20rpx 30rpx;
			border-radius: 20rpx;
			font-size: 18px;
			font-weight: bold;
			margin-left: 30rpx;
		}

		.accent-Tabcolor {
			background-color: #fa3534;
			color: #ffffff;
		}

		.bg-Tabcolor {
			background-color: #ffffff;
			color: #333333;
		}
	}

	// 支付按钮
	.pay-btn-view {
		position: absolute;
		bottom: 80rpx;
		width: 92%;
	}

	.order-pay-view1 {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-bottom: 30rpx;
	}

	.order-pay-image {
		width: 40rpx;
		height: 40rpx;
	}

	.order-pay-imagea {
		display: flex;
		align-items: center;
	}

	.order-pay-box {
		margin-bottom: 30rpx;
		border-bottom: 1rpx solid #DFDFDF;
	}

	.order-pay-box:last-child {
		border-bottom: 0px;
		margin-bottom: 0;
	}

	.order-pay-view {
		background-color: #FFF6DE;
		border-radius: 30rpx;
		padding: 30rpx;

		.order-pay-imageView1 {
			display: flex;
			align-items: center;
			justify-content: center;

			.order-pay-imageView {
				width: 200rpx;
				height: 200rpx;
				border-radius: 20rpx;
			}
		}
	}

	.pay-view {
		padding: 0 30rpx;
		// background-color: #ffffff;
		height: 100vh;
	}
</style>